package com.example.b_308.medical.service;


import com.example.b_308.medical.comment.MedicalApplication;
import com.example.b_308.medical.comment.MedicalContent;
import com.example.b_308.medical.comment.MedicalResult;
import com.example.b_308.medical.comment.net.INetCallBack;
import com.example.b_308.medical.comment.utils.SharedPreferencesUtils;
import com.example.b_308.medical.model.Staff;
import com.example.b_308.medical.view.user.IUserView;

/**
 * Created by B-308 on 2017/11/28.
 */
public class UserService extends BaseService {

    static Staff userDao;
    IUserView iUserView;

    public UserService(IUserView iUserView) {
        this.iUserView = iUserView;
        userDao = SharedPreferencesUtils.getObj(MedicalApplication.getMedicalApplication(), Staff.USER_TAG, Staff.class);
        if (null == userDao) {
            userDao = new Staff();
        }
    }

    public void login(Staff staff) {
        userDao.postData("/doctor/account/login", staff, new INetCallBack() {
            @Override
            public void onNetCallBack(MedicalResult medicalResult) {
                if (MedicalContent.SUCCESS_CODE == medicalResult.code) {
                    userDao = new Staff(medicalResult.data);
                    SharedPreferencesUtils.putObj(MedicalApplication.getMedicalApplication(), Staff.USER_TAG, userDao);
                    iUserView.loginSuccess(userDao);
                } else {
                    iUserView.fail(medicalResult.message);
                }
            }
        });
    }

    public void register(Staff staff) {
        userDao.postData("/doctor/account/register", staff, new INetCallBack() {
            @Override
            public void onNetCallBack(MedicalResult medicalResult) {
                if (MedicalContent.SUCCESS_CODE == medicalResult.code) {
                    userDao = new Staff(medicalResult.data);
                    iUserView.registerSuccess();
                } else {
                    iUserView.fail(medicalResult.message);
                }
            }
        });
    }

    public void getCode(Staff staff) {
        userDao.getRequestHttp("/doctor/account/phone/code", staff, new INetCallBack() {
            @Override
            public void onNetCallBack(MedicalResult medicalResult) {
                if (MedicalContent.SUCCESS_CODE == medicalResult.code) {
                    userDao = new Staff(medicalResult.data);
                    iUserView.getCodeSuccess();
                } else {
                    iUserView.fail(medicalResult.message);
                }
            }
        });
    }

    public void loginDefault() {
        userDao.postData("/doctor/user/loginDefault", userDao, new INetCallBack() {
            @Override
            public void onNetCallBack(MedicalResult medicalResult) {
                if (MedicalContent.SUCCESS_CODE == medicalResult.code) {
                    userDao = new Staff(medicalResult.data);
                } else {
                    iUserView.fail(medicalResult.message);
                }
            }
        });
    }

    public void forgetpwd(Staff staff) {
        userDao.postData("/doctor/account/forgetpwd", staff, new INetCallBack() {
            @Override
            public void onNetCallBack(MedicalResult medicalResult) {
                if (MedicalContent.SUCCESS_CODE == medicalResult.code) {
                    userDao = new Staff(medicalResult.data);
                    iUserView.forgetpwdSuccess();
                } else {
                    iUserView.fail(medicalResult.message);
                }
            }
        });
    }

}
